﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Common;
using Dto;

namespace SqlServerDAL
{
    public class GroupManageDAL
    {
        private const string WAMING = "SqlServerDAL.GroupManageDAL";

        public bool Save(GroupManageDto dto)
        {
            try
            {
                string sql;
                var parameters = new List<SqlParameter>
                                     {
                                         new SqlParameter("@ManageUserId", SqlDbType.Int) {Value = dto.ManageUserId},
                                         new SqlParameter("@ManageObjectId", SqlDbType.Int) {Value = dto.ManageObjectId},
                                         new SqlParameter("@ManageTargetType", SqlDbType.Int)
                                             {Value = dto.ManageTargetType},
                                         new SqlParameter("@ManageTargetId", SqlDbType.Int) {Value = dto.ManageTargetId},
                                         new SqlParameter("@ManageTime", SqlDbType.DateTime) {Value = dto.ManageTime},
                                         new SqlParameter("@ManageGroupId", SqlDbType.Int) {Value = dto.ManageGroupId}
                                     };
                if (dto.ManageId == 0)
                {
                    sql =
                        @"insert into GroupManage(ManageUserId,ManageObjectId,ManageTargetType,ManageTargetId,ManageTime,ManageGroupId) values(
                        @ManageUserId,@ManageObjectId,@ManageTargetType,@ManageTargetId,@ManageTime,@ManageGroupId)";
                }
                else
                {
                    sql =
                        @"update GroupManage set ManageUserId=@ManageUserId,ManageObjectId=@ManageObjectId,ManageTargetType=@ManageTargetType,
                        ManageTargetId=@ManageTargetId,ManageTime=@ManageTime,ManageGroupId=@ManageGroupId where ManageId=@ManageId";
                    parameters.Add(new SqlParameter("@ManageId", SqlDbType.Int) { Value = dto.ManageId });
                }
                SqlHelper.ExecuteNonQuery(SqlHelper.connStr, CommandType.Text, sql, parameters.ToArray());
                return true;
            }
            catch (Exception ex)
            {
                Log.LogHandler.LogError(WAMING + ".Save(GroupManageDto dto)", ex);
                return false;
            }
        }
    }
}
